package com.zhu.function;

import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;

/**
 * 执行python脚本的 process
 * 数据输入 JSONObject  数据的输出类型 jsonObj 中增加一个分词字段 ArrayList [env,srcipt,args[]
 */
@Data
public class PythonScriptExecute implements Serializable {

    //单例
    private static PythonScriptExecute pythonScriptExecute;

    public static Integer scriptSysNum;

    public String[] scriptEnvConfig;

    private PythonScriptExecute(String[] config){
        this.scriptEnvConfig = config;
    }

    public static PythonScriptExecute getInstance(Properties properties) throws IOException {  //外部获取脚本信息

        if(pythonScriptExecute == null){
            String environment = properties.getProperty("pyscript.env");
            String path = properties.getProperty("pyscript.path");
            scriptSysNum = Integer.parseInt(properties.getProperty("pyscript.sys"));
            String[] configArray = new String[scriptSysNum + 2];
            configArray[0] = environment;
            configArray[1] = path;
            pythonScriptExecute = new PythonScriptExecute(configArray);
        }
        return pythonScriptExecute;
    }

    public ArrayList<String> executePythonScript(ArrayList<String> sys) {
        ArrayList<String> resultList = null;
        if (sys.size() != scriptSysNum) {
            throw new RuntimeException("参数传递不符合要求");
        }
        for (int i = 0; i < sys.size(); i++) {
            scriptEnvConfig[i + 2] = sys.get(i);
        }
        //System.out.println(Arrays.toString(scriptEnvConfig));
        try {
            resultList = new ArrayList<>();
            Process process = Runtime.getRuntime().exec(scriptEnvConfig);
            String value = null;
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "gb2312"));
            while ((value = bufferedReader.readLine()) != null) {
                resultList.add(value);
            }
            bufferedReader.close();
            if (process.waitFor() == 0){
              //  System.out.println("脚本执行成功");
              //  System.out.println(resultList);
                return resultList;
            }else {
                throw new RuntimeException("脚本异常");
            }
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
        return null;
    }






    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();
        properties.load(new FileInputStream("D:\\java\\NLP\\Flink-realtimeProcess\\src\\main\\resources\\pyscript.properties"));
       // System.out.println(properties.getProperty("pyscript.env"));
        PythonScriptExecute instance = PythonScriptExecute.getInstance(properties);
        System.out.println(Arrays.toString(instance.scriptEnvConfig));
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("这蛋糕真好吃啊");
        instance.executePythonScript(arrayList);
    }



}
